/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

/*
 * http://www.quesucede.com/page/show/id/conway_game_of_life_android (the tutorial)
 * 
 */

package com.gameoflife;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import java.util.Vector;

/** 
 * Displays a simulation of Conway's Game of Life. Allows for modifications of game settings 
 * through the Preferences system. 
 */
public class MainActivity extends Activity implements OnClickListener{

	/** Holds activities started by the application. Used to call finish() on activities that might
	 *  still be running. */
	public static Vector<Activity> activities = new Vector<Activity>();
	
	/** Called when the activity is first created. Sets up handlers for menu button presses. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // click-handlers for buttons
        View continueButton = findViewById(R.id.continue_button);
        continueButton.setOnClickListener(this);
        View newButton = findViewById(R.id.new_button);
        newButton.setOnClickListener(this);
        View aboutButton = findViewById(R.id.about_button);
        aboutButton.setOnClickListener(this);
        View exitButton = findViewById(R.id.exit_button);
        exitButton.setOnClickListener(this);
    }
    
    /** Starts the appropriate activity for the button pressed, or exits the game. */
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.continue_button:
        	Intent menuIntent = new Intent(this, PreferencesActivity.class);
        	startActivity(menuIntent);
            break;
        case R.id.new_button:
            Intent gridIntent = new Intent(this, GridActivity.class);
            startActivity(gridIntent);
            break;
        case R.id.about_button:
            Intent i = new Intent(this, AboutActivity.class);
            startActivity(i);
            break;
        case R.id.exit_button:
        	for(Activity activity: activities ){
        		activity.finish();
        	}
        	activities.clear();
        	finish();
            break;
        }
    }
    
    /** Logs a message when onDestroy() is called. */
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	Log.i("GameOfLife", "Game of Life Destroyed");
    }
}
